<?php


 //模糊搜索
 function Seach_Area($keyword) {
 	global $db;
 	global $table_top;
 	$Sql = "SELECT * from " . $table_top . "area WHERE name like '%$keyword%'";
 	$return = $db->getone ( $Sql );
 	if (empty ( $return )) {
 		$Sql = "SELECT * from " . $table_top . "area WHERE pinyin = '$keyword'";
 		$return = $db->getone ( $Sql );
 	}
 	return $return;
 }
 //地区下级列表
 function Getone_pArea($cityid) {
 	global $db;
 	global $table_top;
 	$Sql = 'SELECT * from ' . $table_top . 'area WHERE parentid=\'' . $cityid . '\'';
 	$return = $db->select ( $Sql );
 	return $return;
 }
 //地区详情
 function Getone_Area_Pinyin($citypinyin) {
 	global $area_cache_py;
 	return $area_cache_py[$citypinyin];
 }
 //地区详情
 function Getone_Area_Id($cityid) {
 	global $area_cache_id;
 	return $area_cache_id[$cityid];
 }
 //天气详情
 function Getone_Weather($cityid,$cdate) {

 	global $db;
 	global $table_top;
 	!$cdate&&$cdate=date("Y-m-d");
 	$return = $db->getone("SELECT * from {$table_top}weatherlog WHERE `AreaID`='{$cityid}' and `Update`='{$cdate}'");
 	$return2 = $db->getone("SELECT * from {$table_top}weather WHERE `AreaID`='{$cityid}'");
 /*	if (empty ( $return )) {
 		$pcity = Getone_Area_Id ( $cityid );
 		$return = $db->getone("SELECT * from {$table_top}weatherlog WHERE `AreaID`='{$pcity [parentid]}' and `Update`='{$cdate}'");
 		$return2 = $db->getone("SELECT * from {$table_top}weather WHERE `AreaID`='{$pcity [parentid]}'");
 	}
 	$return['WeatherCode']=$return2['WeatherCode'];
 	$return['images']=$return2['images'];
 	$return['ad']=$return2['ad'];
 	return $return;*/
     if(empty ( $return )){
     	$pcity = Getone_Area_Id ( $cityid );
     	if($pcity['Level']==3){
     		$return = $db->getone("SELECT * from {$table_top}weatherlog WHERE `AreaID`='{$pcity [parentid]}' and `Update`='{$cdate}'");
     		$return2 = $db->getone("SELECT * from {$table_top}weather WHERE `AreaID`='{$pcity [parentid]}'");
     		if(empty ( $return )){
     			$return=$return2;
     		}else{
     			$return['WeatherCode']=$return2['WeatherCode'];
     			$return['images']=$return2['images'];
     			$return['ad']=$return2['ad'];
     		}
     	}else {
     		if($pcity['Level']==2){
     			$return = $db->getone("SELECT * from {$table_top}weatherlog WHERE `AreaID`='{$pcity [parentid]}' and `Update`='{$cdate}'");
     			$return2 = $db->getone("SELECT * from {$table_top}weather WHERE `AreaID`='{$pcity [parentid]}'");
     			if(empty ( $return )){
     				if(empty ( $return2 )){
     					$return=$db->getone("SELECT * from {$table_top}weather WHERE `AreaID`='{$cityid}'");
     				}else {
     					$return=$return2;
     				}
     			}else{
     				$return['WeatherCode']=$return2['WeatherCode'];
     				$return['images']=$return2['images'];
     				$return['ad']=$return2['ad'];
     			}
     		}
     	}
     }else {
     	$return['WeatherCode']=$return2['WeatherCode'];
     	$return['images']=$return2['images'];
     	$return['ad']=$return2['ad'];   	
     }
     return $return;
 }
 //天气计数器
 function Add_Hot($cityid,$ziduan) {
 	global $db;
 	global $table_top;
	
 	$time = time ();
 	$db->Update ( "UPDATE " . $table_top . "weather set $ziduan = '$time' where AreaID = '$cityid'" );
 	return $return;
 }
 //片区城市
 function Select_Pianqu_City($GroupID, $limit) {
 	global $db;
 	global $table_top;
 	$Sql = 'SELECT * from ' . $table_top . 'area WHERE GroupID=\'' . $GroupID . '\' and IsTop=1 limit ' . $limit;
 	$return = $db->select ( $Sql );
 	return $return;
 }
 //同城城市
 function Getone_Weather_Other($cityid) {
 	global $db;
 	global $table_top;
 	$return2 = Getone_Area_Id ( $cityid );
 	if ($return2 ['property'] == '2') {
 		$cityid = $return2 ['parentid'];
 	}
 	$Sql = 'SELECT * from ' . $table_top . 'area WHERE parentid=\'' . $cityid . '\'';
 	$return = $db->select ( $Sql );

 	return $return;
 }
 //周边城市
 function Select_Weather_OtherCity($cityid) {
 	global $db;
 	global $table_top;
 	$return2 = Getone_Area_Id ( $cityid );
	
 	if ($return2 ['property'] == '2' || $cityid == '110000' || $cityid == '120000' || $cityid == '310000' || $cityid == '500000') {
 		return false;
 	}
 	$Sql = 'SELECT * from ' . $table_top . 'area WHERE parentid=\'' . $return2 ['parentid'] . '\' and id!=' . $cityid . ' limit 9';
 	$return = $db->select ( $Sql );
 	unset ( $return2, $Sql );
 	return $return;
 }
 //高温排序
 function Select_High($limit) {
 	global $db;
 	global $table_top;
 	//	$Sql = 'SELECT AreaID,high,low from ' . $table_top . 'weather where high!=\'\' and property=1 order by high desc limit ' . $limit;
	$Sql = 'SELECT w.AreaID,w.property,w.high,w.low,a.id,a.Ab,a.name,a.pinyin from ' . $table_top . 'weather as w left join '.$table_top.'area as a on a.id=w.AreaID  where w.high<>"" and w.property=1 order by w.high desc limit ' . $limit;
	

	$return = $db->select($Sql);

	
	
 	/*
 	foreach ( $return as $k => $v ) {
 		$return2 = Getone_Area_Id ( $v ['AreaID'] );
 		$return [$k] ['Ab'] = $return2 ['Ab'];
 		$return [$k] ['name'] = $return2 ['name'];
 		$return [$k] ['pinyin'] = $return2 ['pinyin'];
 		unset ( $return2 );
 		}*/
 	return $return;
 }
 //低温排序
 function Select_Low($limit) {
 	global $db;
 	global $table_top;
 	//	$Sql = 'SELECT AreaID,low,high from ' . $table_top . 'weather where low!=\'\' and property=1 order by low asc limit ' . $limit;
 	$Sql = 'SELECT w.AreaID,w.property,w.high,w.low,a.* from ' . $table_top . 'weather as w left join '.$table_top.'area as a on a.id = w.AreaID  where w.high<>"" and w.property=1 order by w.low desc limit ' . $limit;
 	$return = $db->select ( $Sql );
 	/*
 	foreach ( $return as $k => $v ) {
 		$return2 = Getone_Area_Id ( $v ['AreaID'] );
 		$return [$k] ['Ab'] = $return2 ['Ab'];
 		$return [$k] ['name'] = $return2 ['name'];
 		$return [$k] ['pinyin'] = $return2 ['pinyin'];
 		unset ( $return2 );
 		}*/
 	//print_r($return);exit;
 	return $return;
 }
 //最近关注城市
 function Select_Hot($ziduan,$limit) {
 	global $db;
 	global $table_top;
 	$Sql = 'SELECT AreaID from ' . $table_top . 'weather order by '.$ziduan.' desc limit ' . $limit;
 	//        	       $Sql = 'SELECT w.'.$ziduan.',w.AreaID,a.name,a.Ab,a.pinyin from '.$table_top.'weather as w left join '.$table_top.'area as a on a.id=w.AreaID order by w.'.$ziduan.' desc limit '.$limit;
	//      $Sql = 'SELECT w.*,ar.* from '.$table_top.'weather as w left join '.$table_top.'area as ar w.AreaID=ar.id  limit 1';

 	$return = $db->select ( $Sql );
 	
 	foreach ( $return as $k => $v ) {
 		$return2 = Getone_Area_Id ( $v ['AreaID'] );
 		$return [$k] ['name'] = $return2 ['name'];
		$return [$k] ['Ab'] = $return2 ['Ab'];
		$return [$k] ['pinyin'] = $return2 ['pinyin'];
		unset ( $return2 );
		}
	return $return;
}
//历史气候详情
function Getone_History($cityid, $month, $day) {
	global $db;
	global $table_top;
	$Sql = 'SELECT * from ' . $table_top . 'history WHERE areaid=\'' . $cityid . '\' and month=\'' . $month . '\' and day=\'' . $day . '\'';
	$return = $db->getone ( $Sql );
	return $return;
}
//身份导航
function Select_province($provinceid) {
	global $db;
	global $table_top;
	$Sql = 'SELECT * from ' . $table_top . 'area WHERE parentid=\'' . $provinceid . '\'';
	$return = $db->select ( $Sql );
	foreach ( $return as $k => $v ) {
		$Sql1 = 'SELECT * from ' . $table_top . 'area WHERE parentid=\'' . $v ['id'] . '\'';
		$return1 = $db->select ( $Sql1 );
		$return [$k] ['area'] = $return1;
	}
	unset ( $Sql1, $return1, $Sql, $table_top );
	return $return;
}
//最近关注城市
function Select_QiHou($qihouid, $limit) {
	global $db;
	global $table_top;
	//$Sql = 'SELECT AreaID from ' . $table_top . 'weather where qihou=' . $qihouid . ' limit ' . $limit;
		$Sql = 'SELECT w.AreaID,a.* from ' . $table_top . 'weather as w left join '.$table_top.'area as a on a.id = w.AreaID  where w.qihou='.$qihouid.'  limit ' . $limit;
	$return = $db->select ( $Sql );
	/*
	foreach ( $return as $k => $v ) {
		$return2 = Getone_Area_Id ( $v ['AreaID'] );
		$return [$k] ['name'] = $return2 ['name'];
		$return [$k] ['Ab'] = $return2 ['Ab'];
		$return [$k] ['pinyin'] = $return2 ['pinyin'];
		unset ( $return2 );
	}
	*/
	return $return;
}
$pianqu = Array (0 => Array ("id" => "1000", "name" => "华北" ), 1 => Array ("id" => "1001", "name" => "华东" ), 2 => Array ("id" => "1002", "name" => "华南" ), 3 => Array ("id" => "1003", "name" => "华中" ), 4 => Array ("id" => "1004", "name" => "东北" ), 5 => Array ("id" => "1005", "name" => "西北" ), 6 => Array ("id" => "1006", "name" => "西南" ) );

//城市地图
function Select_AllCity() {
	global $db;
	global $table_top;
	$Sql = "SELECT id,name,pinyin,Ab from " . $table_top . "area WHERE parentid=0 and property=0";
	$return = $db->select ( $Sql );
	foreach ( $return as $k => $v ) {
		$Sql = "SELECT id,name,pinyin,Ab from " . $table_top . "area WHERE parentid=" . $v ['id'];
		$return [$k] ['c'] = $db->select ( $Sql );
		foreach ( $return [$k] ['c'] as $k2 => $v ) {
			$Sql = "SELECT id,name,pinyin,Ab from " . $table_top . "area WHERE parentid=" . $v ['id'];
			$return [$k] ['c'][$k2] ['a'] = $db->select ( $Sql );
		}
	}
	return $return;
}
//城市地图
function Select_AllCity2() {
	global $db;
	global $table_top;
	$Sql = "SELECT id,name,pinyin,Ab from " . $table_top . "area WHERE id='110000' or id='120000' or id='310000' or id='500000'";
	$return = $db->select ( $Sql );
	foreach ( $return as $k => $v ) {
		$Sql = "SELECT id,name,pinyin,Ab from " . $table_top . "area WHERE parentid=" . $v ['id'];
		$return [$k] ['a'] = $db->select ( $Sql );
	}
	return $return;
}
/*
* 取友情链接
* @param int
* @return array
*/
function get_links($id = 0) {
	global $db;
	global $table_top;
	$links = $db->select ( "select title,url from {$table_top}links where displaylocation=1 order by sort asc" );
	return $links;
}
/*
* 取友情链接
* @param int
* @return array
*/
function Select_Links() {
	global $db;
	global $table_top;
	$links = $db->select ( "select title,url from {$table_top}links where displaylocation=2 or displaylocation=0 order by sort asc" );
	return $links;
}
/*
* 取警报
* @param int
* @return array
*/
function Select_JingBao() {
	global $db;
	global $table_top;
	$data = $db->select ( "select `update`,title,url,zt from {$table_top}yujing" );
	foreach($data as $k=>$v){
		$data[$k]['url'] = str_replace ( ".shtml", "", $v['url'] );
	}
	return $data;
}
/*
* 取警报
* @param int
* @return array
*/
function GetOne_JingBao($url) {
	global $db;
	global $table_top;
	$data = $db->getone ( "select * from {$table_top}yujing where url='".$url."'" );
	return $data;
}
/*
* 取实时信息
* @param int
* @return array
*/
function Get_ShiShi($cityid) {
	global $db;
	global $table_top;
	if(empty($cityid)){
		return false;
	}
	$data = $db->getone ( "select * from {$table_top}weatherinfo where cityid=".$cityid );
	return $data;
}
/*
* 取实时信息
* @param int
* @return array
*/
function Select_News($classid,$limit) {
	global $db;
	global $table_top;
	if(empty($classid)){
		return false;
	}
	$data = $db->select ( "select * from {$table_top}news where classid=".$classid." order by id desc limit ".$limit );
	return $data;
}
/*
* 取实时信息
* @param int
* @return array
*/
function Select_News_no($id,$limit) {
	global $db;
	global $table_top;
	if(empty($id)){
		return false;
	}
        $classidduqu = $db->getone ( "select * from {$table_top}news where id=".$id );
		$classiddu = $classidduqu['classid'];
		$data = $db->select ( "select * from {$table_top}news where id!=".$id." and classid=".$classiddu." order by id desc limit ".$limit );
	return $data;
}
/*
* 取实时信息
* @param int
* @return array
*/
function Get_News($id) {
	global $db;
	global $table_top;
	if(empty($id)){
		return false;
	}
	$data = $db->getone ( "select * from {$table_top}news where id=".$id );
	return $data;
}


?>
